package com.customer.crm.system.controller;

import com.customer.crm.system.pojo.SystemUser;
import com.customer.crm.util.ResultData;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;



import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

public abstract class BaseController {
	/**
	 * 把所有的Service注入给BaseController
	 */
//	@Autowired
//	protected IUserService userService;
//	
//	@Autowired
//	protected IRoleService roleService;
//	
//	@Autowired
//	protected IFunctionService funcService;
//	
//	@Autowired
//	protected IDictService dictService;
//	@Autowired
//	protected IDictTypeService dtService;
//	@Autowired
//	protected ITaskService taskService;
//	@Autowired
//	protected IMenuService menuService;
//	@Autowired
//	protected ICustomerService customerService;
//	
//	@Autowired
//	protected IDepartmentService deptService;
//	
//	@Autowired
//	protected IProductService productService;
//	
//	@Autowired
//	protected IBusinessService busiService;
//	
//	@Autowired
//	protected IContactsService ctcsService;
//
//	@Autowired
//	protected IPositionService positionService;
//
//	@Autowired
//	protected IEmployeeService  employeeService;
//	
//
//	@Autowired
//	protected IMarketingService marketingService;
//
//
//	@Autowired
//	protected IEmailService emailService;
//	
//
//	@Autowired
//	protected IContractService contractService;
//	
//	@Autowired
//	protected iMessageService MessageService;
//
//
//	@Autowired
//	protected IMsgService msgService;
//	
//	@Autowired
//	protected IAnnouncementService announcementService;


	/**
	 * 封装响应信息
	 */
	protected ResultData resultData;
	
	/**
	 * 用于接收分页页码
	 */
	protected Long page = 1L;
	
	/**
	 * 记录数
	 */
	protected Long rows = 20L;

	public ResultData getResultData() {
		return resultData;
	}

	public void setResultData(ResultData resultData) {
		this.resultData = resultData;
	}
	
	/**
	 * 获取到当前登录用户的信息
	 * @return
	 */
	protected SystemUser getCurrentUser(HttpServletRequest request){
		HttpSession session = request.getSession();
		return (SystemUser)session.getAttribute("user");
	}
	
	
	public Long getPage() {
		return page;
	}

	public void setPage(Long page) {
		this.page = page;
	}

	public Long getRows() {
		return rows;
	}

	public void setRows(Long rows) {
		this.rows = rows;
	}

	/**
	 * 把对象转换为json格式并且输出至客户端
	 * @param obj
	 * @throws IOException
	 */
	protected void printJSONObject(Object obj,HttpServletResponse response) throws IOException{
		printJSONObject(obj,response, new String[]{});
	}
	
	/**
	 * 将对象转换为json格式并且输出至客户端
	 * @param obj
	 * @param excludeProperties -将会被配置属性
	 * @throws IOException
	 */
	public static void printJSONObject(Object obj,HttpServletResponse response,String... excludeProperties) throws IOException{
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json; charset=UTF-8");
		
		PrintWriter out = response.getWriter();
		
		JsonConfig config = new JsonConfig();
		config.setExcludes(excludeProperties);
		
		JSONObject json = JSONObject.fromObject(obj, config);
		
		out.print(json.toString());
		out.flush();
	}
}
